package com.eastjet.maintenance.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eastjet.maintenance.domain.Documentation;
import com.eastjet.maintenance.domain.Purchasedetails;
import com.eastjet.maintenance.domain.Purchaseorder;
import com.eastjet.maintenance.feign.CodeDataFeing;
import com.eastjet.maintenance.mapper.PurchaseorderMapper;
import com.eastjet.maintenance.service.PurchaseorderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 杨博文
 * @description 针对表【purchaseorder(采购订单)】的数据库操作Service实现
 * @createDate 2024-11-25 09:23:39
 */
@Service
public class PurchaseorderServiceImpl extends ServiceImpl<PurchaseorderMapper, Purchaseorder> implements PurchaseorderService {
    @Autowired
    private PurchaseorderMapper purchaseorderMapper;// 采购订单服务
    @Autowired
    private DocumentationServiceImpl documentationServiceImpl;// 文档服务
    @Autowired
    private PurchasedetailsServiceImpl purchasedetailsServiceImpl;// 采购明细服务
    @Autowired
    private CodeDataFeing codeDataFeing;//自动生成代码
    @Autowired
    private ApplicationServiceImpl applicationServiceImpl;//采购申请服务

    /**
     * 查询采购订单列表
     *
     * @param purchaseorder
     * @return
     */
    @Override
    public List<Purchaseorder> SelectOrderList(Purchaseorder purchaseorder) {
        return purchaseorderMapper.SelectOrderList(purchaseorder);
    }

    /**
     * 新增设备采购订单
     */
    @Override
    public Boolean InsertOrder(Map<String, Object> map) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String code = codeDataFeing.test(new HashMap<String, String>() {{
            put("TableName", "purchaseorder");
        }});
        Purchaseorder p = new Purchaseorder();
        p.setCode(code);
        p.setProcurementTopics(map.get("procurementTopics").toString());
        try {
            p.setDatePurchase(df.parse(map.get("datePurchase").toString()));
            p.setArrival(df.parse(map.get("arrival").toString()));
            p.setCreationTime(df.parse(map.get("creationTime").toString()));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        p.setRelevancy(map.get("relevancy").toString());
        p.setPurchasingStaff(map.get("purchasingStaff").toString());
        p.setDepartment(map.get("department").toString());
        p.setContact(map.get("contact").toString());
        p.setVendor(map.get("vendor").toString());
        p.setMerchant(map.get("merchant").toString());
        p.setContact2(map.get("contact2").toString());
        p.setAffair(map.get("affair").toString());
        p.setState(Integer.valueOf(map.get("state").toString()));
        p.setPan(1);
        p.setRemark(map.get("remark").toString());
        p.setCreatedBy(map.get("createdBy").toString());
        p.setIs_delete(0);
        boolean save = this.save(p);
        if (save) {
            // 新增采购订单明细
            List<Purchasedetails> list = new ArrayList<Purchasedetails>();
            List<Object> purchasedetails = (List<Object>) map.get("tableData");
            purchasedetails.forEach(item -> {
                item = (Map<String, Object>) item;
                Purchasedetails s = new Purchasedetails();
                s.setId(Integer.valueOf(((Map<?, ?>) item).get("id").toString()));
                s.setNumbering(((Map<?, ?>) item).get("numbering").toString());
                s.setName(((Map<?, ?>) item).get("name").toString());
                s.setModel(((Map<?, ?>) item).get("model").toString());
                s.setType(((Map<?, ?>) item).get("type").toString());
                s.setBrand(((Map<?, ?>) item).get("brand").toString());
                s.setQuantity(Integer.valueOf(((Map<?, ?>) item).get("quantity").toString()));
                s.setReference(BigDecimal.valueOf(Double.valueOf(((Map<?, ?>) item).get("reference").toString())));
                s.setEstimate(BigDecimal.valueOf(Double.valueOf(((Map<?, ?>) item).get("estimate").toString())));
                s.setActual(Integer.valueOf(((Map<?, ?>) item).get("actual").toString()));
                s.setActualPrice(BigDecimal.valueOf(Double.valueOf(((Map<?, ?>) item).get("actualPrice").toString())));
                s.setActualTotal(BigDecimal.valueOf(Integer.valueOf(((Map<?, ?>) item).get("actual").toString()) * Double.valueOf(((Map<?, ?>) item).get("actualPrice").toString())));
                s.setRequestNumber(((Map<?, ?>) item).get("requestNumber").toString());
                s.setPurchaseOrder(code);
                s.setRemark(((Map<?, ?>) item).get("remark").toString());
                s.setCreatedBy(map.get("createdBy").toString());
                s.setTombstone(0);
                list.add(s);
            });
            Boolean b = purchasedetailsServiceImpl.UpdatePurchasedetails(list);
            try {
                applicationServiceImpl.UpdteApplicationQi(list.get(0).getRequestNumber(), "3");
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            if (b) {
                // 新增文档
                List<Documentation> docs = new ArrayList<Documentation>();
                List<Object> documentation = (List<Object>) map.get("documents");
                documentation.forEach(item -> {
                    item = (Map<String, Object>) item;
                    Documentation d = new Documentation();
                    d.setCode(code);
                    d.setFilename((String) ((Map<?, ?>) item).get("name"));
                    try {
                        d.setUpdated(df.parse((((Map<?, ?>) item).get("uploadDate")).toString()));
                        d.setCreationTime(df.parse((((Map<?, ?>) item).get("uploadDate")).toString()));
                    } catch (ParseException e) {
                        throw new RuntimeException(e);
                    }
                    d.setUploader(((Map<?, ?>) item).get("uploader").toString());
                    d.setSize(((Map<?, ?>) item).get("size").toString());
                    d.setCreatedBy(((Map<?, ?>) item).get("uploader").toString());
                    d.setTombstone(0);
                    docs.add(d);
                });
                return documentationServiceImpl.InsertList(docs);
            }

        }
        return null;
    }

    /**
     * 编辑采购订单
     */
    @Override
    public Boolean UpdateOrder(Map<String, Object> map) {
        return null;
    }

    /**
     * 删除采购订单
     */
    @Override
    public Boolean DeleteOrder(List<String> id) {
        return this.removeByIds(id);
    }

}




